/***************************
* 1.新闻列表
* 2.新闻详情
****************************/

/* 变量 */
var news = "news/news.html";
var newsDetail = "news/newsDetail.html";
var latestActivity = "news/latestActivity.html";
var latestActivityDetail = "news/latestActivityDetail.html";

/* 新闻 */
var contactsCallback_news = myApp.onPageInit('news', function (page) {

    var type = ISS.utils.getParameter("type");
    
    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var index_tab4 = 1;
    var index_tab5 = 1;
    var index_tab6 = 1;
    var current_tabindex = 0;
    var size = 10;
    // 加载flag
    var loading = false;
    if(type === '2'){
        $$(".sub_buttons_row a").eq(0).removeClass("active");
        $$(".sub_buttons_row a").eq(2).addClass("active");
        $$("#sub_news_tab1").removeClass("active");
        $$("#sub_news_tab3").addClass("active");        
        current_tabindex = 2;        
    }
    LoadNewsAll(index_tab1, size);
    //加载数据
    function LoadNewsAll(pageindex, pagesize) {

        // 检索条件
        var v_search = [];
        var v_order = [];

        var Identifier = "";
        if (current_tabindex == 0) {
            Identifier = "dynamic/news";
        }
        else if (current_tabindex == 1) {
            Identifier = "dynamic/policiesregulations";
        }else if(current_tabindex==2){
            Identifier="dynamic/travelassociation";
        }else if(current_tabindex==3){
            Identifier="dynamic/scenicline";
        }else if(current_tabindex==4){
            Identifier="dynamic/servicecentre";
        }else if(current_tabindex==5){
            Identifier="dynamic/investmentproject";
        }

        var conditionParams = JSON.stringify({
            type: 0,
            Identifier: Identifier,
            pageIndex: pageindex,
            pageSize: pagesize,
        });

        myApp.showPreloader();
        $$.ajax({
            url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
            crossDomain: true,
            data: { content: conditionParams },
            dataType: "jsonp",
            method: "POST",
            success: function (data, status) {
                data = JSON.parse(data);
                myApp.hidePreloader();
                loading=false;
                if (data && data.results.length > 0) {
                    for (var i = 0; i < data.results.length; i++) {

                        var timer = data.results[i].CreateDate.split(" ");
                        if (timer.length > 0) {
                            data.results[i].CreateDate = timer[0];
                        }
                        if (data.results[i].F_LargeImage == undefined ||
                            data.results[i].F_LargeImage == null || data.results[i].F_LargeImage == "") {
                            data.results[i].F_LargeImage = NoPicture();
                        };
                    };

                    var searchTemplate = $$('#template-news-tab1').html();
                    if (searchTemplate) {
                        var compiledTemplate = Template7.compile(searchTemplate);
                        var html = compiledTemplate(data);
                        if (current_tabindex == 0) $$("#news_page #sub_news_tab1 .content-block ul").append(html);
                        else if (current_tabindex == 1) $$("#news_page #sub_news_tab2 .content-block ul").append(html);
                        else if (current_tabindex == 2) $$("#news_page #sub_news_tab3 .content-block ul").append(html);
                        else if (current_tabindex == 3) $$("#news_page #sub_news_tab4 .content-block ul").append(html);
                        else if (current_tabindex == 4) $$("#news_page #sub_news_tab5 .content-block ul").append(html);
                        else if (current_tabindex == 5) $$("#news_page #sub_news_tab6 .content-block ul").append(html);
                        flag_InfiniteScroll = flag_needScroll = true;
                    }

                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;

                        
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }

                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }

                            if (true) { // 只有一个TAB,所以不需要
                                // 如果正在加载，则退出
                                if (loading) return;
                                // 设置flag
                                loading = true;
                                // 模拟1s的加载过程
                                setTimeout(function () {
                                    
                                    if (current_tabindex == 0) {
                                        index_tab1++;
                                        LoadNewsAll(index_tab1, pagesize);
                                    }
                                    else if (current_tabindex == 1) {
                                        index_tab2++;
                                        LoadNewsAll(index_tab2, pagesize);
                                    }
                                    else if (current_tabindex == 2) {
                                        index_tab3++;
                                        LoadNewsAll(index_tab3, pagesize);
                                    }
                                    else if (current_tabindex == 3) {
                                        index_tab4++;
                                        LoadNewsAll(index_tab4, pagesize);
                                    }
                                    else if (current_tabindex == 4) {
                                        index_tab5++;
                                        LoadNewsAll(index_tab5, pagesize);
                                    }
                                    else if (current_tabindex == 5) {
                                        index_tab6++;
                                        LoadNewsAll(index_tab6, pagesize);
                                    }

                                }, 100);
                            }
                        });
                    }
                    if (current_tabindex == 0 && ($$("#news_page #sub_news_tab1 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab1 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 1 && ($$("#news_page #sub_news_tab2 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab2 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader2').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 2 && ($$("#news_page #sub_news_tab3 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab3 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 3 && ($$("#news_page #sub_news_tab4 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab4 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader4').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 4 && ($$("#news_page #sub_news_tab5 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab5 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader5').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 5 && ($$("#news_page #sub_news_tab6 .content-block ul li").length >= data.total || $$("#news_page #sub_news_tab6 .content-block ul li").length < pagesize)) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader6').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        $$("#news_page #sub_news_tab1 .content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab1 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 1) {
                        $$('.infinite-scroll-preloader2').remove();
                        $$("#news_page #sub_news_tab2 .content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab2 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 2) {
                        $$('.infinite-scroll-preloader3').remove();
                        $$("#news_page #sub_news_tab3 .content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab3 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 3) {
                        $$('.infinite-scroll-preloader4').remove();
                        $$("#news_page #sub_news_tab4.content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab4 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 4) {
                        $$('.infinite-scroll-preloader5').remove();
                        $$("#news_page #sub_news_tab5 .content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab5 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 5) {
                        $$('.infinite-scroll-preloader6').remove();
                        $$("#news_page #sub_news_tab6 .content-block .noAnswer").remove();
                        if (data.total == 0)
                            $$("#news_page #sub_news_tab6 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                }
            },
            error: function (xhr, status) {
                myApp.hidePreloader();
                myApp.alert("服务器连接失败！");
                loading=false;
            }
        });
    }

    //切换
    $$("#news_page #sub_buttons_row a").on("click", function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();
        myApp.attachInfiniteScroll($$('.infinite-scroll'));

        flag_InfiniteScroll = true;
        flag_needScroll = false;
        if ($$(this).index() == 0 && $$("#news_page #sub_news_tab1 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab1, size);
        }
        else if ($$(this).index() == 1 && $$("#news_page #sub_news_tab2 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab2, size);
        }
        else if ($$(this).index() == 2 && $$("#news_page #sub_news_tab3 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab3, size);
        }
        else if ($$(this).index() == 3 && $$("#news_page #sub_news_tab4 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab4, size);
        }
        else if ($$(this).index() == 4 && $$("#news_page #sub_news_tab5 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab5, size);
        }
        else if ($$(this).index() == 5 && $$("#news_page #sub_news_tab6 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab6, size);
        }
    });
});

/* 新闻详情  */
var contactsCallback_newsDetail = myApp.onPageInit('newsDetail', function (page) {
    //获取页面参数
    var scode = ISS.utils.getParameter("scode");
    if (page != undefined) {
        scode = page.query.scode;
    }
    var _content = JSON.stringify({
        type: 1,
        Code: scode
    })

    $$.ajax({
        url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
        crossDomain: true,
        data: { "content": _content },
        dataType: "jsonp",
        method: "POST",
        success: function (data, status) {
            data = JSON.parse(data);
            if (data && data.status == "0") {
                var searchTemplate = $$('#template-news-detail').html();
                var compiledSearchTemplate = Template7.compile(searchTemplate);
                var html = compiledSearchTemplate(data.results[0]);
                $$("#news_detail_page .page-content").html(html);
            }
        },
        error: function (xhr, status) {
            console.log("ajax error!");
        }
    });
});

/* 最新活动 */
var contactsCallback_latestActivity = myApp.onPageInit('latestActivity', function (page) {

    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var current_tabindex = 0;
    var size = 10;
    // 加载flag
                        var loading = false;

    LoadNewsAll(index_tab1, size);
    //加载数据
    function LoadNewsAll(pageindex, pagesize) {

        // 检索条件
        var v_search = [];
        var v_order = [];

        var Identifier = "know/dynamic/activity";

        var conditionParams = JSON.stringify({
            type: 0,
            Identifier: Identifier,
            pageIndex: pageindex,
            pageSize: pagesize,
        });

        $$.ajax({
            url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
            crossDomain: true,
            data: { content: conditionParams },
            dataType: "jsonp",
            method: "POST",
            success: function (data, status) {
                data = JSON.parse(data);
                loading=false;
                if (data && data.results && data.results.length > 0) {
                    for (var i = 0; i < data.results.length; i++) {

                        var timer = data.results[i].CreateDate.split(" ");
                        if (timer.length > 0) {
                            data.results[i].CreateDate = timer[0];
                        }
                        if (data.results[i].F_LargeImage == undefined ||
                            data.results[i].F_LargeImage == null || data.results[i].F_LargeImage == "") {
                            data.results[i].F_LargeImage = NoPicture();
                        };
                    };

                    var searchTemplate = $$('#template-latestActivity-tab1').html();
                    if (searchTemplate) {
                        var compiledTemplate = Template7.compile(searchTemplate);
                        var html = compiledTemplate(data);
                        $$("#latestActivity_page .content-block ul").append(html);
                        flag_InfiniteScroll = flag_needScroll = true;
                    }

                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;

                        
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }

                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }

                            if (true) { // 只有一个TAB,所以不需要
                                // 如果正在加载，则退出
                                if (loading) return;
                                // 设置flag
                                loading = true;
                                // 模拟1s的加载过程
                                setTimeout(function () {
                                   
                                    index_tab1++;
                                    LoadNewsAll(index_tab1, pagesize);

                                }, 100);
                            }
                        });
                    }
                    if ($$("#latestActivity_page .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    $$('.infinite-scroll-preloader1').remove();
                    $$("#latestActivity_page .content-block .noAnswer").remove();
                    if (data.total == 0)
                        $$("#latestActivity_page .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");

                }
            },
            error: function (xhr, status) { 
                loading=false;
            }
        });
    }
});

/* 最新活动详情  */
var contactsCallback_latestActivityDetail = myApp.onPageInit('latestActivityDetail', function (page) {
    //获取页面参数
    var scode = ISS.utils.getParameter("scode");
    if (page != undefined) {
        scode = page.query.scode;
    }
    var _content = JSON.stringify({
        type: 1,
        Code: scode
    })

    $$.ajax({
        url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
        crossDomain: true,
        data: { "content": _content },
        dataType: "jsonp",
        method: "POST",
        success: function (data, status) {
            data = JSON.parse(data);
            if (data && data.status == "0") {
                var searchTemplate = $$('#template-latestActivity-detail').html();
                var compiledSearchTemplate = Template7.compile(searchTemplate);
                var html = compiledSearchTemplate(data.results[0]);
                $$("#latestActivityDetail_page .page-content").html(html);
            }
        },
        error: function (xhr, status) {
            console.log("ajax error!");
        }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(news) > -1) {
        contactsCallback_news.trigger();
    }
    else if (href.indexOf(newsDetail) > -1) {
        contactsCallback_newsDetail.trigger();
    }
    else if (href.indexOf(latestActivity) > -1) {
        contactsCallback_latestActivity.trigger();
    }
    else if (href.indexOf(latestActivityDetail) > -1) {
        contactsCallback_latestActivityDetail.trigger();
    }
}

initData();

